<?php
$command = isset($_POST['command'])?$_POST['command']:"none";
$url = url('admin','groups-manager');
if(isset($_POST['btSubmit'])) {
    unset($_POST['btSubmit']);
    unset($_POST['command']);
    
    if($command == "none"){
        header("location: {$url}");
        exit(0);
    }
    
    if($command == "add"){
        $url = url('admin','groups-add');
        header("location: {$url}");
        exit(0);
    }
    /**
     * Lấy dữ liệu từ form
     */
    $data = array();
    foreach($_POST as $id=>$status){
        if($status == "on"){
            $data[] = $id;
        }
    }
    
    /**
     * Xóa item đã check
     */
    if($command == "delete"){
        foreach($data as $id){
            $Bus->Group->deleteItemWithId($id);
        }
        header("location: {$url}");
        exit(0);
    }
}
if($command == "edit"){
    if(isset($_POST['btUpdate'])){
        unset($_POST['btUpdate']);
        unset($_POST['command']);
        $data =array();
        
        foreach($_POST as $id=>$name){
            $data[] = $id;
        }
        
        foreach($_POST as $id=>$name){
            if(empty($name)){
                $error = "Tên nhóm không được để trống.";
            }else{
                $result = $Bus->Group->update(array('groups_name'=>$name),"idgroups='{$id}'");
                if(!$result){
                    $error = "Tên nhóm <span style='color:red;'>{$name}</span> đã tồn tại";
                    break;
                }
            }
        }
        
        if(!isset($error)){
            header("location: {$url}");
            exit(0);
        }
    }
    include 'component/header.php';
    include 'component/slidebar.php';?>
    
    <!-- BEGIN CONTENT -->
    <div id="content">
        <div class="box-out">
        	<div class="box-in">
        		<div class="box-head"><h1>Cập nhật nhóm người dùng...</h1></div>
        		<div class="box-content">
                    <?php if(isset($error)) {?>
                    <div class="notification error">
        				<div class="messages"><?php echo $error; ?><div class="close"><img src="css/images/icon/close.png" alt="close" /></div></div>
        			</div><!-- end div .notification error -->
                    <?php }?>
                    
        			<div class="form">
        				<form action="" method="post" >
                            <input type="hidden" name="command" value="edit" />
        					<fieldset>
                                <?php foreach($data as $id) {
                                    $group = $Bus->Group->getItemById($id);
                                    ?>
        						<label for="small_field">Tên nhóm</label>
        						<input type="text" class="text small" id="small_field" name="<?php echo $group->idgroups;?>" value="<?php echo $group->groups_name; ?>" />
                                <?php } ?>
        						<hr />
        						<input type="submit" value="Cập nhật" class="submit" name="btUpdate" />
                                <a class="submit" href="<?php echo url('admin','groups-manager');?>">Về trang quản lý</a>
        					</fieldset>
        				</form>
        			</div><!-- end div .form -->
        		</div><!-- end div .box-content -->
        	</div><!-- end div .box-in -->
    </div><!-- end div #content -->
    <!-- END CONTENT -->
    
    <?php include 'component/footer.php';
}?>
